//------------------------------------------------------------------------------
// <copyright file="LoadImageWithoutLockFileConverter.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using DotBeer.Business.Entities;

namespace DotBeer.UI.WPFClient
{
    public class LoadImageWithoutLockFileConverter : IValueConverter
    {
        #region IValueConverter Members

        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                BitmapImage bitmap = new BitmapImage();
                MemoryStream ms = new MemoryStream();

                Bitmap bmp = new Bitmap(value.ToString());
                bmp.Save(ms, ImageFormat.Png);
                bmp.Dispose();

                bitmap.BeginInit();
                bitmap.StreamSource = ms;
                bitmap.EndInit();
                
                return bitmap;
            }
            catch
            {
                return new BitmapImage(new Uri(ApplicationPaths.QuestionMark));
            }
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
